package com.cw.lang.common.wrapper;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cw.lang.common.dto.BaseDto;
import com.cw.lang.common.model.BaseDo;

import cn.hutool.core.util.StrUtil;

/**
 * 构建BaseDo
 *
 * @author chenw
 * @date 2021/11/10
 */
public class BaseWrapper<T extends BaseDo> {
    public void buildBaseWrapper(LambdaQueryWrapper<T> lambdaQueryWrapper, BaseDto baseDto) {
        if (null != baseDto.getId()) {
            lambdaQueryWrapper.eq(T::getId, baseDto.getId());
        }
        if (StrUtil.isNotBlank(baseDto.getIds())) {
            String[] ids = baseDto.getIds()
                .split(",");
            List<String> idList = Arrays.asList(ids);
            List<Long> longIds = idList.stream()
                .map(id -> Long.parseLong(id))
                .collect(Collectors.toList());
            lambdaQueryWrapper.in(T::getId, longIds);
        }
        if (null != baseDto.getCreateBy()) {
            lambdaQueryWrapper.eq(T::getCreateBy, baseDto.getCreateBy());
        }
        if (null != baseDto.getCreateDateStart()) {
            lambdaQueryWrapper.ge(T::getCreateDate, baseDto.getCreateDateStart());
        }
        if (null != baseDto.getCreateDateEnd()) {
            lambdaQueryWrapper.le(T::getCreateDate, baseDto.getCreateDateEnd());
        }
        if (null != baseDto.getUpdateBy()) {
            lambdaQueryWrapper.eq(T::getUpdateBy, baseDto.getUpdateBy());
        }
        if (null != baseDto.getUpdateDateStart() && null != baseDto.getUpdateDateEnd()) {
            lambdaQueryWrapper.ge(T::getUpdateDate, baseDto.getUpdateDateStart());
        }
        if (null != baseDto.getUpdateDateEnd() && null != baseDto.getUpdateDateEnd()) {
            lambdaQueryWrapper.le(T::getUpdateDate, baseDto.getUpdateDateEnd());
        }
    }
}
